import { _decorator, Component, director, Label, Node, ProgressBar, Sprite, SpriteFrame, tween, Vec3 } from 'cc';
import { Core } from './Core/Core';
import { BattleManager } from './BattleManager';
import { EEvent, EPlayerType } from './Config/Enum';
import { ResourceManager } from './Core/ResourceManager';
import { PicPath } from './Config/ResourceConfig';
import ObjectPoolManager from './Core/ObjectPoolManager';
import { Util } from './Util';
const { ccclass, property } = _decorator;


// Battle场景的Header
@ccclass('Header')
export class Header extends Component {

    //节点
    ndPause: Node = null!; // 暂停节点
    ndSpeed: Node = null!; // 加速节点
    lbMoney: Label = null!; // 铜钱标签
    //lbPeople: Label
    currentSpeed: number = 1; // 当前速度
    ndStatistics: Node
    numOfPlayers: number = 1; // 玩家数量

    damageTotal: number = 0; // 总伤害
    damagePercent: number = 0; // 伤害百分比

    // 存储转换后的玩家数组
    selectedPlayersArray: string[] = [];

    protected onLoad(): void {
        console.log("Battle Header Onload")
        //节点
        this.ndPause = this.node.getChildByName("Pause");
        this.ndSpeed = this.node.getChildByName("Speed");
        this.lbMoney = this.node.getChildByName("Gold").getChildByName("Label").getComponent(Label);
        //this.lbPeople = this.node.getChildByName("People").getChildByName("Value").getComponent(Label);


        


        this.ndStatistics = this.node.getChildByName("Statistics");
        
        // 将Set转换为数组
        this.selectedPlayersArray = Array.from(Core.Instance.selectedPlayers);
        this.numOfPlayers = this.selectedPlayersArray.length;
        
        console.log("numOfPlayers", this.numOfPlayers);
    }

    protected onEnable(): void {
        Core.Instance.event.on(EEvent.DamageUpdateHeader, this.onDamageUpdateHeader, this);
        Core.Instance.event.on(EEvent.MoneyUpdateHeader, this.onMoneyUpdateHeader, this);

    }

    protected onDisable(): void {
        Core.Instance.event.off(EEvent.DamageUpdateHeader, this.onDamageUpdateHeader, this);
        Core.Instance.event.off(EEvent.MoneyUpdateHeader, this.onMoneyUpdateHeader, this);
    }

    onDamageUpdateHeader() {
        // 更新统计数据
        for (let i = 0; i < this.numOfPlayers; i++) {
            // 使用转换后的数组获取玩家类型
            let playerType = this.selectedPlayersArray[i];
            let playerName = "Player" + (i + 1);
            let ndPlayer = this.ndStatistics.getChildByName(playerName);

            // 更新伤害百分比
            this.damagePercent = BattleManager.Instance.playerDamageMap.get(playerType as EPlayerType) / BattleManager.Instance.damageTotal * 100;
            ndPlayer.getChildByName("Damage").getChildByName("Label_Damage").getComponent(Label).string = this.damagePercent.toFixed(1) + "%";
            ndPlayer.getChildByName("Damage").getChildByName("ProgressBar").getComponent(ProgressBar).progress = this.damagePercent/100
        }
    }

    onMoneyUpdateHeader(){

        // tween(this.lbMoney.node)
        //     .to(0.1, { scale: new Vec3(1.2, 1.2, 1) }) // 放大
        //     .to(0.1, { scale: new Vec3(0.9, 0.9, 1) }) // 缩小
        //     .to(0.1, { scale: new Vec3(1, 1, 1) })     // 恢复原状
        //     .call(() => {
        //         this.lbMoney.string = BattleManager.Instance.gold.toString(); // 更新铜钱显示
        //     })
        //     .start();

        Util.tweenBigSmall(this.lbMoney.node, () => {
            this.lbMoney.string = BattleManager.Instance.gold.toString(); // 更新铜钱显示
        })

        

    }

    setSpeed(speed) {
        this.ndSpeed.getChildByName("Number").getComponent(Label).string = "X" + speed
        
    }




    start() {
        // 隐藏其他玩家的统计数据
        for (let i = this.numOfPlayers; i < 5; i++) {
            let playerName = "Player" + (i + 1);
            let ndPlayer = this.ndStatistics.getChildByName(playerName);
            ndPlayer.active = false;
        }

        // 新玩家隐藏按钮
        if (Core.Instance.isNewPlayer) {
            this.ndPause.active = false; // 隐藏暂停按钮
            this.ndSpeed.active = false; // 隐藏加速按钮
        }

        // 显示玩家的图片
        for (let i = 0; i < this.numOfPlayers; i++) {
            // 使用转换后的数组获取玩家类型
            let playerType = this.selectedPlayersArray[i];
            let playerName = "Player" + (i + 1);
            let ndPlayer = this.ndStatistics.getChildByName(playerName);
            //console.log("ResourceManager.Instance.getRes<SpriteFrame>(PicPath[playerType )", ResourceManager.Instance.getRes<SpriteFrame>(PicPath[playerType + "IdleFront"]+"/Idle_1"));
            //console.log("PicPath[playerType +", PicPath[playerType + "Idle"]);
            ndPlayer.getChildByName("Avatar").getComponent(Sprite).spriteFrame = 
                //Core.Instance.textureMap.get(playerType + "AttackFront")[0];
                ResourceManager.Instance.getRes<SpriteFrame>(PicPath[playerType + "IdleFront"]+"/Idle_1")
                //ResourceManager.Instance.getRes<SpriteFrame>(EPath.PIC_SKILL+player+'Skill'+i+'/icon')
        }
    }

}